<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use addons\shopro\model\User;
use addons\shopro\model\UserLevel;
use think\Db;

class Goods extends Base
{

    protected $noNeedLogin = ['index', 'detail', 'lists', 'activity', 'seckillList', 'grouponList', 'store','after_sale'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        // 测试腾讯云短信
        $user = \addons\shopro\model\User::get(10011);
        $walletLog = \addons\shopro\model\UserWalletLog::get(61);

        $user && $user->notify(
            new \addons\shopro\notifications\Wallet([
                'walletLog' => $walletLog,
                'event' => 'wallet_change'
            ])
        );
    }

    public function detail()
    {
        $id = $this->request->get('id');

        $user = $this->auth->getUserinfo();
        $discount = $user['level']['discount_price'];
        $discount = bcdiv($discount, '10', 2);

        $detail = \addons\shopro\model\Goods::getGoodsDetail($id);
        $detail['price'] = bcmul($detail['price'], $discount, 2);
        if($detail['sku_price']){
            $sku_price = $detail['sku_price'];
            foreach ($sku_price as $key => &$value) {
                $value['original_price'] = $value['price'];
                $value['price'] = bcmul($value['price'], $discount, 2);
            }
            $detail['sku_price'] = $sku_price;
        }


        // 记录足记
        \addons\shopro\model\UserView::addView($detail);

        $sku_price = $detail['sku_price'];      // 处理过的规格
        // tp bug json_encode 或者 toArray 的时候 sku_price 会重新查询数据库，导致被处理过的规格又还原回去了
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;


        $goods_consent=Db::name('config')->where('name','goods_consent')->find();
        $detail['goods_consent'] = '';
        if(!empty($goods_consent))$detail['goods_consent'] = $goods_consent['value'];

        //地址
        $user = User::info();
        $detail['province_name'] = '';
        $detail['city_name'] = '';
        $detail['area_name'] = '';
        $detail['address'] = '';

        if($user){
            $detail['is_consent'] = $user->is_consent;//是否同意
            $address=Db::name('shopro_user_address')->where('user_id',$user->id)->find();
            if(!empty($address)){
                $detail['province_name'] = $address['province_name'];
                $detail['city_name'] = $address['city_name'];
                $detail['area_name'] = $address['area_name'];
                $detail['address'] = $address['address'];
            }
        }


        $ask=Db::name('ask')
            ->where('goods_id',$detail['id'])
            ->order('weigh desc')
            ->limit(3)
            ->select();
        $detail['ask']=array();
        if(!empty($ask))$detail['ask']=$ask;


        $this->success('商品详情', $detail);
    }

    public function lists()
    {
        $params = $this->request->get();
        $user = $this->auth->getUser();
        if(!empty($user->id)){
            $user_id=$user->id;
            $params['user_id']=$user_id;

            $_user = $this->auth->getUserinfo();
            $discount = $_user['level']['discount_price'];
            $params['discount'] = bcdiv($discount, '10', 2);
        }else{
            $params['user_id']=0;
            $params['discount'] = '1';
        }



        $data = \addons\shopro\model\Goods::getGoodsList($params);

        $this->success('商品列表', $data);
    }


    /**
     * 获取商品支持的 自提点
     */
    public function store()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsStore($params);

        $this->success('自提列表', $data);
    }


    // 秒杀列表
    public function seckillList() {
        $params = $this->request->get();

        $this->success('秒杀商品列表', \addons\shopro\model\Goods::getSeckillGoodsList($params));
    }


    // 拼团列表
    public function grouponList() {
        $params = $this->request->get();

        $this->success('拼团商品列表', \addons\shopro\model\Goods::getGrouponGoodsList($params));
    }


    public function activity()
    {
        $activity_id = $this->request->get('activity_id');
        $activity = \addons\shopro\model\Activity::get($activity_id);
        if (!$activity) {
            $this->error('活动不存在', null, -1);
        }
        
        $goods = \addons\shopro\model\Goods::getGoodsList(['goods_ids' => $activity->goods_ids]);
        $activity->goods = $goods;
        
        $this->success('活动列表', $activity);
    }

    public function favorite()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserFavorite::edit($params);
        $this->success($result ? '收藏成功' : '取消收藏', $result);
    }

    public function favoriteList()
    {
        $data = \addons\shopro\model\UserFavorite::getGoodsList();
        $this->success('商品收藏列表', $data);
    }


    public function viewDelete()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserView::del($params);
        $this->success('删除成功', $result);
    }


    public function viewList()
    {
        $data = \addons\shopro\model\UserView::getGoodsList();
        $this->success('商品浏览列表', $data);
    }

    public function ask_list()
    {
        $params = $this->request->get();

        $where = [];
        if(!empty($params['goods_id'])){
            $where['a.goods_id'] = $params['goods_id'];
        }
        $ask=Db::name('ask')->alias('a')
            ->join('ask_desc ad','ad.ask_id=a.id','left')
            ->where($where)
            ->field('a.id,a.title,a.count,ad.name as d_name,ad.image as d_image,ad.title as d_title')
            ->order('a.weigh desc')
            ->select();

        $data=array();
        foreach ($ask as $v){
            $i=0;
            foreach ($data as $key=>$d){
                if($d['id']==$v['id']){
                    $desc_info=array();
                    $desc_info['d_title']=$v['d_title'];
                    $desc_info['d_name']=$v['d_name'];
                    $desc_info['d_image']=$v['d_image'];
                    $data[$key]['desc_list'][]=$desc_info;
                    $i=1;
                }
            }
            if($i==0){
                $info=array();
                $info['id']=$v['id'];
                $info['title']=$v['title'];
                $info['count']=$v['count'];
                $info['is_show']=0;
                $info['desc_list']=array();
                if(!empty($v['d_title'])){
                    $desc_info=array();
                    $desc_info['d_title']=$v['d_title'];
                    $desc_info['d_name']=$v['d_name'];
                    $desc_info['d_image']=$v['d_image'];
                    $info['desc_list'][]=$desc_info;
                }
                $data[]=$info;
            }
        }

        $this->success('商品问大家列表', $data);
    }

    public function ask_add()
    {
        $params = $this->request->post();
        if(empty($params['goods_id']))$this->error('请选择商品');
        if(empty($params['message']))$this->error('请提问');

        $user = $this->auth->getUser();

        $insData=array();
        $insData['goods_id']=$params['goods_id'];
        $insData['user_id']=$user['id'];
        $insData['title']=$params['message'];
        $insData['createtime']=time();
        $insData['updatetime']=time();
        $res = Db::name('ask')->insert($insData);

        $this->success('商品问大家', '');
    }

    public function user_consent()
    {
        $user = User::info();
        $insData=array();
        $insData['is_consent']=1;
        $res=Db::name('user')->where('id', $user->id)->update($insData);
        $this->success('商品同意条款');
    }

    public function get_user_consent()
    {
        $user = User::info();

        $data=array();
        $data['is_consent']=0;
        $data['goods_consent'] = '';
        $goods_consent=Db::name('config')->where('name','goods_consent')->find();
        if(!empty($goods_consent))$data['goods_consent'] = $goods_consent['value'];
        $res=Db::name('user')->where('id', $user->id)->find();
        if(!empty($res))$data['is_consent'] = $res['is_consent'];

        $this->success('商品同意条款',$data);
    }

    public function after_sale()
    {
        $data='';
        $after_sale=Db::name('config')->where('name','after_sale')->find();
        if(!empty($after_sale))$data = $after_sale['value'];

        $this->success('售后保障',$data);
    }

}
